/*
 * Copyright (c) 2020 IUV Inc. All rights reserved.
 * 项目名称：IUV TXY
 * 文件名：UserController.java
 * Date：20-10-22 下午1:36
 * Author：Will
 */

package com.iuv.imdemo.controller;

import com.iuv.common.CommonOperateCode;
import com.iuv.imdemo.domain.DemoImUserInfo;
import com.iuv.imdemo.service.ImAccountService;
import com.iuv.imdemo.service.UserInfoService;
import com.iuv.imdemo.util.RespondUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * UserController
 *
 * @author Will
 * @date 13:36
 */
@RequestMapping("/user")
@Controller
public class UserController {
    public final static String SESSION_USER_ID="user_id";
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private ImAccountService imAccountService;
    /**
     * 用户登录
     * @param userId
     * @param password
     * @return
     */
    @RequestMapping("userLogin")
    @ResponseBody
    public Map<String, Object> userLogin(@RequestParam String userId, @RequestParam String password, HttpServletRequest request) {
        userId= StringUtils.trimToNull(userId);
        if(userId==null)
        {
            return RespondUtil.respondFail(CommonOperateCode.SysCode.USER_ID_NOT_NULL, CommonOperateCode.SysMsg.USER_ID_NOT_NULL);
        }
        // 通过用户ID查询用户信息
        DemoImUserInfo demoImUserInfo=userInfoService.queryUserInfoByUserId(userId);

        if (demoImUserInfo == null) {
            return RespondUtil.respondFail(CommonOperateCode.SysCode.ACCOUNT_NOT_MATCH, CommonOperateCode.SysMsg.ACCOUNT_NOT_MATCH);
        }

        /**
         * TODO 问题四 通过密码判断用户登录是否成功，失败返回 PASSWORD_NOT_MATCH 编码和信息
         * 1. 用户密码不能为空
         * 2. 用户密码需要和数据库中一致
         * 3. 数据库中密码保存为密文，使用md5进行加密 DigestUtils.md5Hex(明文)
         */
        password=StringUtils.trimToNull(password);
        if(password==null)
        {
            return RespondUtil.respondFail(CommonOperateCode.SysCode.PASSWORD_NOT_NULL, CommonOperateCode.SysMsg.PASSWORD_NOT_NULL);
        }
        if(!StringUtils.equals(DigestUtils.md5Hex(password),demoImUserInfo.getPassword()))
        {
            return RespondUtil.respondFail(CommonOperateCode.SysCode.PASSWORD_NOT_MATCH, CommonOperateCode.SysMsg.PASSWORD_NOT_MATCH);
        }

        /**
         * END
         */
        Map<String, Object> result= RespondUtil.respondSuccess("", "");
        result.put("userSig",imAccountService.genUserSig(userId));
        request.getSession().setAttribute(SESSION_USER_ID,userId);
        return result;
    }

    /**
     * 模糊查询用户列表（添加好友用）
     * @param key
     * @return
     */
    @RequestMapping("queryUserLike")
    @ResponseBody
    public List<String> queryUserLike(String key) {
        return userInfoService.queryAllUserIdMatch(key);
    }

}
